import moment from 'moment'
export const formatDate = (time = new Date(), fmt = 'yyyy/MM/dd hh:mm' ) => {
  if(!time) return ''
  if(typeof time == "string"){
    time = time.replace(/-/g, '/')
  }
  let date = new Date(time)
  var o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

// 获取某天前后的日期
export const GetDateStr = (AddDayCount=0, time = new Date()) => {
  var dd = new Date(time);
  dd.setDate(dd.getDate() + AddDayCount)
  return formatDate(dd)
}


// 获取周一
export const getFirstWeek = () => {
  return moment().day(1).format('YYYY/MM/DD')
}
// 获取周日
export const getLastWeek = () => {
  const dd = new Date()
  const day = dd.getDay() == 0 ? 7 : dd.getDay()
  return GetDateStr(7-day)
}
// 获取周六
export const getSatWeek = () => {
  const dd = new Date()
  const day = dd.getDay() == 0 ? 7 : dd.getDay()
  return GetDateStr(6-day)
}

// 下周一
export const getNextMonday = () => {
  const dd = new Date()
  const day = dd.getDay() == 0 ? 7 : dd.getDay()
  return GetDateStr(7-day+1)
}
// 下周日
export const getNextSunday = () => {
  const dd = new Date()
  const day = dd.getDay() == 0 ? 7 : dd.getDay()
  return GetDateStr(7-day+7)
}
// 上周一
export const getLastMonday = () => {
  const dd = new Date()
  const day = dd.getDay() == 0 ? 7 : dd.getDay()
  return GetDateStr(-(day - 1) -7)
}
// 上周日
export const getLastSunday = () => {
  const dd = new Date()
  const day = dd.getDay() == 0 ? 7 : dd.getDay()
  return GetDateStr(7-day-7)
}

// 本月第一天
export const getFirstOneDay = (data = new Date()) => {
  if(typeof data == "string" && data.indexOf('-') != -1  && data.split('-').length ===2){
    data = data.replace(/-/g, '/') + '/01'
  }
  if(typeof data == "string" && data.indexOf('/') != -1  && data.split('/').length ===2 ){
    data  = data +'/01'
  }
  const dd = new Date(data)
  dd.setDate(1)
  return formatDate(dd)
}
function getSetDay(year, month){
  if (month != 2) {
    if (
      month == 4 ||
      month == 6 ||
      month == 9 ||
      month == 11
    ) {
      return 30;
    } else {
      return  31;
    }
  } else {
    if (
      (year % 4 == 0 && year % 100 != 0) ||
      year % 400 == 0
    ) {
      return  29;
    } else {
      return  28;
    }
  }
}
// 本月最后一天
export const getLastOneDay = (data = new Date()) => {
  if(typeof data == "string"  && data.indexOf('-') != -1 && data.split('-').length ===2){
    let arr = data.split('-')
    data = data.replace(/-/g, '/') + '/'+getSetDay(parseInt(arr[0]), parseInt(arr[1]))
    const dd = new Date(data)
    return formatDate(dd)
  }
  if(typeof data == "string"  && data.indexOf('/') != -1  && data.split('/').length ===2 ){
    let arr = data.split('/')
    data = data+'/'+getSetDay(parseInt(arr[0]), parseInt(arr[1]))
  }
  const dd = new Date(data)
  dd.setDate(1);
  dd.setMonth(dd.getMonth()+1);//这时候day已经变成下个月第一天
  dd.setDate(dd.getDate() - 1);//下个月的第一天的前一天就是本月最后一天
  return formatDate(dd)
}

// 上月第一天
export const getLastMonthFirstOneDay = () => {
  let startDate = moment().month(moment().month() - 1).startOf('month').format('YYYY/MM/DD')
  return startDate
}
// 上月最后一天
export const getLastMonthEndDay = () => {
  const endDate = moment().month(moment().month() - 1).endOf('month').format('YYYY/MM/DD')
  return endDate
}
// 下月第一天
export const getNextMonthFirstOneDay = () => {
  let startDate = moment().month(moment().month() + 1).startOf('month').format('YYYY/MM/DD')
  return startDate
}

// 上季度第一天
export const getLastQuarterFirstDay = () => {
  const startDate = moment().quarter(moment().quarter() - 1).startOf('quarter').format('YYYY/MM/DD');
  return startDate
}
// 上季度最后一天
export const getLastQuarterLastDay = () => {
  const endDate = moment().quarter(moment().quarter() - 1).endOf('quarter').format('YYYY/MM/DD');
  return endDate
}

//获取本季度的开始时间
export const getFirstDayOfQuarter = () => {
  const startTime = moment(moment().quarter(moment().quarter()).startOf('quarter').valueOf()).format('YYYY/MM/DD');
  return startTime
}
//获取本季度的结束时间
export const getLastDayOfQuarter = () => {
  const endTime = moment(moment().quarter(moment().quarter()).endOf('quarter').valueOf()).format('YYYY/MM/DD');
  return endTime
}

// 获取一天开始时间
export const curStartTime = (date = new Date()) => {
  if(typeof date == "string" && date.indexOf('-')!= -1){
    date = date.replace(/-/g, '/')
    if(date.split('/').length == 2){
      date = date+'/01'
    }
  }
  if(typeof data == "string" && date.indexOf('/') != -1 && date.split('/').length ===2 ){
    date = date+'/01'
  }
  let dd = new Date(date)
  return parseInt(new Date( dd.setHours(0, 0, 0, 0) ).getTime() / 1000 + '')
}

// 获取一天结束时间
export const curEndTime = (date = new Date()) => {
  if(typeof date == "string" && date.indexOf('-') != -1){
    let arr = date.split('-')
    date = date.replace(/-/g, '/')
    if(date.split('/').length == 2){
      date = date+'/'+getSetDay(parseInt(arr[0]), parseInt(arr[1]))
    }
  }
  if(typeof date == "string" && date.indexOf('/')!= -1  && date.split('/').length ===2 ){
    let arr = date.split('/')
    date = date+'/'+getSetDay(parseInt(arr[0]), parseInt(arr[1]))
  }
  let dd = new Date(date)
  return parseInt(new Date( dd.setHours(23,59,59,999) ).getTime()  / 1000 + '')
}

// 时间转换成时间戳（精确到秒）
export const timeToStamp = (time) => {
  if(!time) return ''
  if(typeof time == "string"){
    time = time.replace(/-/g, '/')
  }
  let d = new Date(time);
  return parseInt(d.getTime() / 1000 + '');
}


// 时间戳转时间
export const getDateHMs = (time) => {
  let time1 = formatTime(time);
  return formatDate(time1, 'yyyy/MM/dd hh:mm:ss')
}
const formatTime = function (time) {
  if (!time) return;
  let timeStr = time + '000';
  let timeNew = parseInt(timeStr);
  return new Date(timeNew)
}

/**
 * 根据当月获取上个月
 * ‘2022-1'
 */
export const getMonthGetLastMonth = (month = new Date()) => {
  return moment(month).subtract(1, 'months').format('YYYY-MM')
}
/**
 * 根据当月获取下个月
 * ‘2022-1'
 */
export const getMonthGetNextMonth = (month = new Date()) => {
  return moment(month).add(1, 'months').format('YYYY-MM')
}